<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<link rel="STYLESHEET" type="text/css" href="./styles.css" /></head>
<body>

<!-- ===================================================================== -->
<!-- = XML information goes here                                         = -->
<!-- ===================================================================== -->
<lzelement title="Script">
    <lztier>Language Feature</lztier>
    <lzcategory>Scripting</lzcategory>
    <lzshortdesc>
        A block of JavaScript.
    </lzshortdesc>
    <lztag>tag-script.xml</lztag>
</lzelement>

<p>The <code>script</code> element contains JavaScript code that is
executed when the application is loaded.  This element must be at the
<code>canvas</code> level; that is, it cannot be contained within any
subordinate tag. If the <attribute>src</attribute> attribute is
present, it names a JavaScript file whose contents are compiled into
the application.</p>

<p>
In the example below, we add a method to the built-in
<code>Array</code> class that will let us find the index of elements
in an array. Note that <code>Array.find</code> uses <code>===</code>
for finding, so that two objects that are similar will not be
confused. This is why looking for <code>{example: 'sneaky'}</code>
finds nothing, whereas looking for <code>sneaky</code> succeeds.
</p>

<example>
&lt;canvas debug="true" height="200" width="400"&gt;
  &lt;script&gt;
  &lt;![CDATA[
    // Add a find method to Array
    Array.prototype.find = function ( what ) {
      for (i in this ) {
        if (this[i] === what) {
          return i;
        }       
      }
    }
    
    sneaky = {example: 'sneaky'};
    tryit = ['foo', 42, sneaky, Math.PI, false];
    
    Debug.write("42 is at: " + tryit.find(42));
    Debug.write("false is at: " + tryit.find(false));
    Debug.write("'bar' is at: " + tryit.find('bar'));        
    Debug.write("{example: 'sneaky'} is at: " + tryit.find({example: 'sneaky'}));        
    Debug.write("sneaky is at: " + tryit.find(sneaky));        
  ]]&gt;
  &lt;/script&gt;
&lt;/canvas&gt;
</example> 

</body>
</html>
<!-- * X_LZ_COPYRIGHT_BEGIN ***************************************************
* Copyright 2001-2004 Laszlo Systems, Inc.  All Rights Reserved.              *
* Use is subject to license terms.                                            *
* X_LZ_COPYRIGHT_END ****************************************************** -->
